package com.citycloud.ccuap.tc.admin.config;

import org.apache.catalina.Context;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.web.embedded.tomcat.TomcatContextCustomizer;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

import java.io.File;

/**
 * @author xuhan  build  2020/4/23
 */
@ConditionalOnProperty(name="server.tomcat.docBase")
@Configuration
public class MyTomcatContextCustomizer implements TomcatContextCustomizer {

    private  Logger logger = LoggerFactory.getLogger(getClass());

    @Value("${server.tomcat.docBase:}")
    private String docBase;

    @Override
    public void customize(Context context) {
        logger.debug("自定义tomcat context 修改docBase为指定目录");
        
        if(!StringUtils.isEmpty(docBase)){
            File file = new File(docBase);
            if(!file.exists()){
                file.mkdir();
            }
            context.setDocBase(docBase);
        }
    }
}
